;(function($){
	$.fn.spa_payment_other = function(opts){
		var el = this;
		var opt = {
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.bigTypeHtml();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//数据整理
			el.find("#btnMiscagain").unbind("click").click(function(){
				t.excuteMiscagain();
			})
			
			//关闭返回
			el.find("#btnClose,#close-dialog").unbind("click").click(function(){
				el.modal("hide");
				param.closeBack();
				el.empty();
			});
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div id="modal-dialog" class="modal-dialog" style="width:800px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
//										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
//											'<span class="white">&times;</span>'+
//										'</button>'+
//										'<span id="title-dialog">'+$.i18n.propJsp(param.title)+'</span>'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="title-dialog">'+$.i18n.propJsp("结帐")+'</span>'+
										'<i class="ace-icon fa fa-angle-double-right"></i>'+
										'<span id="ins_argjson_some">NO:'+this.param.no+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="min-height: 100px;">'+
									'<div class="widget-box transparent row">'+
//										'<div class="row">'+
											'<div class="col-sm-12" style="border-bottom:1px solid #428bca;">'+
											'</div>'+
											'<div class="col-sm-12 no-padding" style="display:flex;">'+
												'<div id="bigTypeInfo" style="width:105px;display:flex;flex-direction:column;border-right:1px solid #cccccc;padding:3px;">'+
													
												'</div>'+
												'<div id="checkoutInfo" style="flex:1;padding:3px;">'+
												
												'</div>'+
											'</div>'+
//										'</div>'+
									'</div>'+
								'</div>'+
								'<div class="modal-footer">'+
								
									'<div class="clearfix">'+
										'<div class="pull-left">'+
											'<div class="btn-group">'+
												'<button id="btnMiscagain" class="btn btn-info">'+
													'<i clas="ace-icon fa fa-eye"></i>'+$.i18n.propJsp("数据整理")+
												'</button>'+
											'</div>'+
										'</div>'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="btnClose" class="btn btn-default">'+
													'<i clas="ace-icon fa fa-eye"></i>'+$.i18n.propJsp("关闭")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
									
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		bigTypeHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//大类HTML
			var bigTypeHtml = ''
			$(param.bigType).each(function(k,v){
				var primary = "";
				if(k==0){
					primary = "btn-primary";
				}
				bigTypeHtml += 	'<div class="btn-group">'+
									'<button data-type="'+v.paytype+'" class="btn btn-sm bigtype '+primary+'" style="margin:1px;width:97px;height:38px;">'+
										v.payname+
									'</button>'+
								'</div>';
			})
			el.find("#bigTypeInfo").html(bigTypeHtml);
			
			//绑定大类事件
			el.find(".bigtype").unbind("click").click(function(){
				el.find(".bigtype.btn-primary").removeClass("btn-primary");
				$(this).addClass("btn-primary");
				var paytype = $(this).data("type");
				t.infoHtml(paytype)
			});
			
			el.find(".bigtype.btn-primary").trigger("click");
		},
		infoHtml : function(paytype){
			var el = this.el;
			var param = this.param;
			var t = this;
			
    		var filter = "select * from json where (paytype=='"+(paytype||param.bigType[0].paytype)+"')";
    		var js = jsonsql.query( filter,param.ds);
    		console.log(JSON.stringify(js))
    		var infoHtml = '';
    		$(js).each(function(k,v){
    			infoHtml += '<div class="btn-group">'+
								'<button data-type="'+v.paytype+'" data-text="'+v.text+'" data-code="'+v.dcode+'" data-paytype="'+ v.paytype +'" data-type="'+ v.crtype +'" class="btn btn-sm detailInfo" style="margin:1px;width:97px;height:38px;">'+
									v.text+
								'</button>'+
							'</div>';
    		});
    		el.find("#checkoutInfo").html(infoHtml);
    		
    		el.find(".detailInfo").unbind("click").click(function(){
    			var title = $(this).text();//.replace("支付","") + "支付";
				var bh = $(this).attr("data-code");
				var bhText = $(this).attr("data-text");
				var mc = $(this).text();
				if($(this).attr("data-paytype") == "WC"){//微信支付
//					alert(202+":"+mc+"==="+$(this).attr("data-paytype"));
					$(this).modalFrame("destroy");
					$(this).modalFrame("init",function(modalEle){
						el.modal("toggle");
						$("#"+modalEle).modal("show"); 
						if(!$.isFunction($().spa_wechatAlipayPay)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jquery.modal.spa_wechatAlipayPay.js");
				            $.ajaxSetup({ async: true });
						}
						$("#"+modalEle).spa_wechatAlipayPay({
							title:title?title:"支付方式",//标题
						    price:param.price||"",//支付价钱
						    togatherPrice:param.togatherPrice,
						    nos : param.nos,
						    nos_checkout:param.nos_checkout,
						    bh : bh,
						    bhText : bhText,
						    operation:param.operation,
						    xhs:param.xhs,
							returnBack:function(){
								el.modal("toggle");
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide"); 
							},
							getDefaultBack:function(){
								el.modal("toggle");
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide"); 
//								jf_initSettleAccounts();
								el.find("#btnClose").trigger("click");
								param.dataBack();
							}
						});
					});
				}else if($(this).attr("data-paytype") == "AR"){//AR账
//					alert(228+":"+mc+"==="+$(this).attr("data-paytype"));
					$(this).modalFrame("destroy");
					$(this).modalFrame("init",function(modalEle){
						el.modal("toggle");
						$("#"+modalEle).modal("show"); 
						if(!$.isFunction($().spa_ARAccountPay)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jquery.modal.spa_ARAccountPay.js");
				            $.ajaxSetup({ async: true });
						}
						$("#"+modalEle).spa_ARAccountPay({
							no:(param.no||""),//no
							mc:mc?mc:"",//名称
							bh : bh,
						    bhText : bhText,
							title:title?title:"支付方式",//标题
						    price:param.price,//支付价钱
						    togatherPrice:param.togatherPrice,
						    nos : param.nos,
						    nos_checkout:param.nos_checkout,
						    operation:param.operation,
						    xhs:param.xhs,
							returnBack:function(){
								el.modal("toggle");
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide"); 
							},
							getDefaultBack:function(data){
								el.modal("toggle");
								if(data.morepayment=="NO"){
									$("#"+modalEle).modal("hide"); 
									$("#"+modalEle).empty();
									param.printBillBack(data);
									el.find("#btnClose").trigger("click");
								}else{
									$("#"+modalEle).empty();
									$("#"+modalEle).modal("hide"); 
									param.dataBack();
									el.find("#btnClose").trigger("click");
								}
							}
						});
					});
				}else if($(this).attr("data-paytype") == "RM"){//客账
//					alert(265+":"+mc+"==="+$(this).attr("data-paytype"));
					$(this).modalFrame("destroy");
					$(this).modalFrame("init",function(modalEle){
						el.modal("toggle");
						$("#"+modalEle).modal("show"); 
						if(!$.isFunction($().spa_guestAccountPay)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jquery.modal.spa_guestAccountPay.js");
				            $.ajaxSetup({ async: true });
						}
						$("#"+modalEle).spa_guestAccountPay({
							no:(param.no||""),//no
							mc:mc?mc:"",//名称
							bh:"887",//编号
						    bhText : bhText,
							title:title?title:"支付方式",//标题
						    price:param.price,//支付价钱
						    togatherPrice:param.togatherPrice,
						    nos : param.nos,
						    nos_checkout:param.nos_checkout,
						    operation:param.operation,
						    xhs:param.xhs,
							returnBack:function(){
								el.modal("toggle");
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide"); 
							},
							dataBack : function(data){
								if(data.morepayment=="NO"){
									$("#"+modalEle).modal("hide"); 
									$("#"+modalEle).empty();
									param.printBillBack(data);
									el.find("#btnClose").trigger("click");
								}else{
									$("#"+modalEle).empty();
									$("#"+modalEle).modal("hide");
									param.dataBack();
									el.find("#btnClose").trigger("click");
								}
							},
						});
					});
				}else if($(this).attr("data-paytype") == "TR"){//余额转账
//					alert(303+":"+mc+"==="+$(this).attr("data-paytype"));
					$(this).modalFrame("destroy");
					$(this).modalFrame("init",function(modalEle){ 
						el.modal("toggle");
						$("#"+modalEle).modal("show"); 
						if(!$.isFunction($().balance_transfer)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jquery.modal.balance_transfer.js");
				            $.ajaxSetup({ async: true });
						}
						$("#"+modalEle).balance_transfer({
							no:(param.no||""),//no
							mc:mc?mc:"",//名称
							bh:"887",//编号
						    bhText : bhText,
							title:title?title:"支付方式",//标题
						    price:param.price,//支付价钱
						    togatherPrice:param.togatherPrice,
						    nos : param.nos,
						    nos_checkout:param.nos_checkout,
						    operation:param.operation,
						    xhs:param.xhs,
							returnBack:function(){
								el.modal("toggle");
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide"); 
								el.find("#btnClose").trigger("click");
							},
							dataBack:function(data){
								el.modal("toggle");
								if(data.morepayment=="NO"){
									$("#"+modalEle).modal("hide"); 
									$("#"+modalEle).empty();
//									$(".modal-backdrop").remove();
//									$("#global_printBill").trigger("click");
//									initPageShowOption("选台");
									param.printBillBack(data);
									el.find("#btnClose").trigger("click");
								}else{
									$("#"+modalEle).empty();
									$("#"+modalEle).modal("hide");
//									jf_initSettleAccounts();
									param.dataBack();
									el.find("#btnClose").trigger("click");
								}
							}
						});
					});
				}else{//XX结账方式
//					alert(341+":"+mc+"==="+$(this).attr("data-paytype"));
					$(this).modalFrame("destroy");
					$(this).modalFrame("init",function(modalEle){ 
						el.modal("toggle");
						$("#"+modalEle).modal("show"); 
						if(!$.isFunction($().spa_generalPay)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jQuery.modal.spa_generalPay.js");
				            $.ajaxSetup({ async: true });
						}
						$("#"+modalEle).spa_generalPay({
							no:(param.no||""),//no
							bh:bh?bh:"",//编号
						    bhText : bhText,
							mc:mc?mc:"",//名称
							title:title?title:"支付方式",//标题
						    price:param.price,//支付价钱
						    togatherPrice:param.togatherPrice,
						    nos : param.nos,
						    nos_checkout:param.nos_checkout,
						    operation:param.operation,
						    xhs:param.xhs,
							returnBack:function(){
								el.modal("toggle");
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide");
								param.closeBack();
							},
							dataBack : function(data){
								if(data.morepayment=="NO"){
									$("#"+modalEle).modal("hide"); 
									$("#"+modalEle).empty();
									param.printBillBack(data);
									el.find("#btnClose").trigger("click");
								}else{
									$("#"+modalEle).empty();
									$("#"+modalEle).modal("hide");
									param.dataBack();
									el.find("#btnClose").trigger("click");
								}
							},
						});
					});
				}
    		});
		},
		excuteMiscagain : function(){//procPOSOrderUo_Discount
			var el = this.el;
			var param = this.param;
			var t = this;
			t.wait();
			var arg = {"arg":{"miscagain":"yes"},"ct_brzd":{"no":param.no}}
			jQuery.ajax({  
	    		type : 'POST',
	    		url :  $.HSAPI_AJAXURL+"/qms/mobile/procedure2",  
	    		data : $.HSAPI_ASKJSONINFO( JSON.stringify(arg),'POSOrderUo_Discount',"{}"),
	    		dataType : 'json',  
	    		success : function(data){
	    			t.goon();
	    			if(data.RETURN==1){
	    				jf_success(data.MSG);
	    			}else{
	    				jf_alert(data.MSG);
	    			}
	    		},  
	    		error : function(data){
	    			t.goon();
	    			jf_alert(JSON.stringify(data));
	    		}  
	    	});
		},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
			
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		}
	}
	
})(jQuery);